package com.weiku.config;


import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SimpleScheduleBuilder;
import org.quartz.SimpleTrigger;
import org.quartz.TriggerBuilder;
import org.quartz.impl.StdSchedulerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import com.weiku.task.ListTask;
import com.weiku.task.OrderTask;
import com.weiku.task.PaybillTask;
import com.weiku.task.WaybillTask;

@Configuration
@Order
@Component
public class TaskRunner implements CommandLineRunner {
	//海关路径
	@Value("${localhost.dingdan.xml.path}")
	private  String localhost_dingdan;
	@Value("${localhost.qingdan.xml.path}")
	private String localhost_qingdan;
	@Value("${localhost.yundan.xml.path}")
	private String localhost_yundan;
	@Value("${localhost.zhifudan.xml.path}")
	private String localhost_zhifudan;
    //固定路径
	@Value("${haiguan.dingdan.xml.path}")
	private String haiguan_dingdan;
	@Value("${haiguan.qingdan.xml.path}")
	private String haiguan_qingdan;
	@Value("${haiguan.yundan.xml.path}")
	private String haiguan_yundan;
	@Value("${haiguan.zhifudan.xml.path}")
	private String haiguan_zhifudan;
	

	public void run(String... args) throws Exception {
	   System.out.println(">>>>>>>>>>>>>>>任务服务启动执行 ...<<<<<<<<<<<<<");
	  //创建一个jobDetail的实例，将该实例与HelloJob Class绑定
     JobDetail job_order = JobBuilder.newJob(OrderTask.class)
     		.withIdentity("order")
     		.usingJobData("localhost_dingdan", localhost_dingdan)
     		.usingJobData("haiguan_dingdan", haiguan_dingdan)
     		.build();
     JobDetail job_waybill = JobBuilder.newJob(WaybillTask.class)
     		.withIdentity("waybill")
     		.usingJobData("localhost_yundan", localhost_yundan)
     		.usingJobData("haiguan_yundan", haiguan_yundan)// 定义属性
     		.build();
     JobDetail job_list = JobBuilder.newJob(ListTask.class)
     		.withIdentity("list")
     		.usingJobData("localhost_qingdan", localhost_qingdan)
     		.usingJobData("haiguan_qingdan", haiguan_qingdan)
     		.build();
     JobDetail job_paybill = JobBuilder.newJob(PaybillTask.class)
     		.withIdentity("paybill")
     		.usingJobData("localhost_zhifudan", localhost_zhifudan)
     		.usingJobData("haiguan_zhifudan", haiguan_zhifudan)// 定义属性
     		.build();
     
     
     SimpleTrigger trigger_order = TriggerBuilder.newTrigger()
     		.withIdentity("order")
     		.startNow()
     		.withSchedule(SimpleScheduleBuilder
	        				.simpleSchedule()
	        				.withIntervalInSeconds(10)
	        				.repeatForever())
     		.build();	
     SimpleTrigger trigger_waybill = TriggerBuilder.newTrigger()
      		.withIdentity("waybill")
      		.startNow()
      		.withSchedule(SimpleScheduleBuilder
 	        				.simpleSchedule()
 	        				.withIntervalInSeconds(10)
 	        				.repeatForever())
      		.build();	
     SimpleTrigger trigger_list = TriggerBuilder.newTrigger()
      		.withIdentity("list")
      		.startNow()
      		.withSchedule(SimpleScheduleBuilder
 	        				.simpleSchedule()
 	        				.withIntervalInSeconds(10)
 	        				.repeatForever())
      		.build();	
     SimpleTrigger trigger_paybill = TriggerBuilder.newTrigger()
      		.withIdentity("paybill")
      		.startNow()
      		.withSchedule(SimpleScheduleBuilder
 	        				.simpleSchedule()
 	        				.withIntervalInSeconds(10)
 	        				.repeatForever())
      		.build();	
     //创建schedule实例
     StdSchedulerFactory factory = new StdSchedulerFactory();
     Scheduler scheduler = factory.getScheduler();
     scheduler.start();
   
     scheduler.scheduleJob(job_order,trigger_order);
     scheduler.scheduleJob(job_waybill,trigger_waybill);
     scheduler.scheduleJob(job_paybill,trigger_paybill);
     scheduler.scheduleJob(job_list,trigger_list);
 }
}
